package com.coca1cole.user.controller;

import com.coca1cole.bean.Result;
import com.coca1cole.redis.util.RedisShareLockUtil;
import com.coca1cole.redis.util.RedisUtil;
import com.coca1cole.user.entity.dto.UserDto;
import com.coca1cole.user.entity.dto.UserPageDto;
import com.coca1cole.user.entity.req.UserPageReq;
import com.coca1cole.user.entity.req.UserReq;
import com.coca1cole.user.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private RedisShareLockUtil redisShareLockUtil;
    @GetMapping("/testRedisLock")
    public void testRedisLock() {
        boolean result = redisShareLockUtil.lock("测试", "1231231", 100000L);
        System.out.println(result);
    }
    @GetMapping("/testRedis")
    public String testRedis() {
        redisUtil.set("name", "哈哈");
        return redisUtil.get("name");
    }

    @PostMapping
    public String addUser(@RequestBody UserReq userReq){
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(userReq,userDto);
        userService.addUser(userDto);
        return "1";
    }

    @GetMapping("/getUserPage")
    public Result getUserPage(@RequestBody UserPageReq userPageReq) {
        UserPageDto userPageDto = new UserPageDto();
        BeanUtils.copyProperties(userPageReq, userPageDto);
        return Result.ok(userService.getUserPage(userPageDto));
    }
    @DeleteMapping("/deleteUser/{id}")
    public Result deleteUser(@PathVariable Integer id) {
        return Result.ok(userService.deleteUser(id));
    }

    @GetMapping("test")
    public String test(){
        return "hello word";
    }
}
